package fun.tan90.base.util;

import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
import lombok.AccessLevel;
import lombok.RequiredArgsConstructor;

import java.math.RoundingMode;
import java.util.Objects;

@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
public class AccuracyUtil {

    /**
     * GPS
     */
    public static Double gpsStrToDouble(String gps) {
        if (StrUtil.isBlank(gps)) {
            return null;
        }
        return Double.parseDouble(gps);
    }

    /**
     * 处理风向和流向
     * 向下取整
     */
    public static Integer downDirectionInteger(Double direction) {
        if (Objects.isNull(direction)) {
            return null;
        }
        return NumberUtil.round(direction, 0, RoundingMode.DOWN).intValue();
    }

    /**
     * 处理风速和流速
     * 四舍五入保留4位小数
     */
    public static Double halfUpSpeedDouble(Double speed) {
        if (Objects.isNull(speed)) {
            return null;
        }
        return NumberUtil.round(speed, 4, RoundingMode.HALF_UP).doubleValue();
    }

    /**
     * 心率取整
     */
    public static Integer downHeartrateInteger(Double heartrate) {
        if (Objects.isNull(heartrate)) {
            return -1;
        }
        return NumberUtil.round(heartrate, 0, RoundingMode.DOWN).intValue();
    }

    /**
     * 角度
     * 四舍五入
     */
    public static Double halfUpAngeDouble(Double angle, int scale) {
        if (Objects.isNull(angle)) {
            return null;
        }
        return NumberUtil.round(angle, scale, RoundingMode.HALF_UP).doubleValue();
    }
}
